package cn.yuanqiao.web.controller.archive.appManager;

import cn.yuanqiao.archive.appManager.domain.TenantApply;
import cn.yuanqiao.archive.appManager.service.ITenantApplyService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * applymodelController
 *
 * @author 赵常飞
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/archive/applymodel")
public class TenantApplyController extends BaseController
{
    @Autowired
    private ITenantApplyService tenantApplyService;

    /**
     * 查询applymodel列表
     */
    //@RequiresPermissionsissions("archive:applymodel:list")
    @GetMapping("/list")
    public TableDataInfo list(TenantApply yqdaApplyModel)
    {
        startPage();
        List<TenantApply> list = tenantApplyService.selectYqdaApplyModelList(yqdaApplyModel);
        return getDataTable(list);
    }

    /**
     * 导出applymodel列表
     */
    //@RequiresPermissionsissions("archive:applymodel:export")
    //@Log(title = "applymodel", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantApply yqdaApplyModel)
    {
        List<TenantApply> list = tenantApplyService.selectYqdaApplyModelList(yqdaApplyModel);
        ExcelUtil<TenantApply> util = new ExcelUtil<TenantApply>(TenantApply.class);
        util.exportExcel(response, list, "applymodel数据");
    }

    /**
     * 获取applymodel详细信息
     */
    //@RequiresPermissionsissions("archive:applymodel:query")
    @Log(title = "获取应用定义详细信息",businessType = BusinessType.SELECT)
    @GetMapping(value = "/{menuId}/{categoryId}")
    public AjaxResult getInfo(@PathVariable("menuId") Long menuId,@PathVariable("categoryId") Long categoryId)
    {
        return success(tenantApplyService.selectYqdaApplyModelById(menuId,categoryId));
    }

    /**
     * 档案门类应用定义 添加/修改/添加模块
     * @author syd
     */
    //@RequiresPermissionsissions("archive:applymodel:add")
    @Log(title = "档案门类应用定义添加/修改(添加模块)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TenantApply yqdaApplyModel)
    {
        int i = tenantApplyService.insertYqdaApplyModel(yqdaApplyModel);
        if(i==-1){
            throw new ServiceException("已添加的模块不可重复添加");
        }
        return toAjax(i);
    }

    /**
     * 修改applymodel
     */
    //@RequiresPermissionsissions("archive:applymodel:edit")
    //@Log(title = "applymodel", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TenantApply yqdaApplyModel)
    {
        return toAjax(tenantApplyService.updateYqdaApplyModel(yqdaApplyModel));
    }

    /**
     * 档案门类应用定义删除模块
     * @author syd
     */
    //@RequiresPermissionsissions("archive:applymodel:remove")
    @Log(title = "档案门类应用定义删除模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryId}/{menuId}")
    public AjaxResult remove(@PathVariable Long categoryId,@PathVariable Long menuId)
    {
        return toAjax(tenantApplyService.deleteYqdaApplyModel(categoryId,menuId));
    }

    //@RequiresPermissionsissions("archive:applymodel:clonefield")
    @Log(title = "克隆字段/排序到其他菜单", businessType = BusinessType.OTHER)
    @PostMapping("/clonefield")
    public AjaxResult clonefield(@RequestBody JSONObject json)
    {
        int clonefield = tenantApplyService.clonefield(json);
        if(clonefield==-1){
            throw new ServiceException("模块未添加的不能复制");
        }
        return toAjax(clonefield);
    }

    /**
     * 档案门类应用定义查询已添加的模块
     * @author syd
     */
    //@RequiresPermissionsissions("archive:applymodel:list")
    @Log(title = "档案门类应用定义查询已添加的模块",businessType = BusinessType.SELECT)
    @PostMapping("/listmenu")
    public TableDataInfo listmenu(@RequestBody JSONObject json)
    {
        startPage();
        String menuName=json.getString("menuName");
        if("".equals(menuName)||null==menuName){
            menuName=null;
        }
        List<Map<String,String>> list = tenantApplyService.selectYqdaApplyModelListmenu(menuName,json.getLong("categoryId"));
        return getDataTable(list);
    }

    /**
     * 档案门类应用定义 获取所有菜单
     * @author syd
     * @param
     * @return
     */
    @Log(title = "档案门类应用定义获取所有菜单",businessType = BusinessType.SELECT)
    @GetMapping("/listAllMenu/{categoryId}")
    public TableDataInfo listAllMenu(@PathVariable("categoryId") Long categoryId)
    {
        List<Map<String,String>> list = tenantApplyService.listAllMenu(categoryId);
        return getDataTable(list);
    }

    /**
     * 档案门类应用定义查询已添加的明细
     * @author syd
     */
    //@RequiresPermissionsissions("archive:applymodel:list")
    @Log(title = "档案门类应用定义查询已添加的明细",businessType = BusinessType.SELECT)
    @PostMapping("/fieldlist")
    public AjaxResult getfieldlist(@RequestBody JSONObject json)
    {
        JSONObject returnjson=new JSONObject();
        returnjson.put("field",tenantApplyService.getfieldlist(json));
        return success(returnjson);
    }

    @Log(title = "档案门类应用定义删除模块", businessType = BusinessType.DELETE)
    @PostMapping("/deletebycatemenu")
    public AjaxResult deletebycatemenu(@RequestBody com.alibaba.fastjson.JSONObject json)
    {
        return toAjax(tenantApplyService.deletebycatemenu(json));
    }

}
